import store from "../../store/index.js";
import router from "../../router/index.js";
import axios from "axios";
import {
  Loading
} from "element-ui";
import Message from '@/assets/js/optimizePop.js'
let protocol = window.location.protocol
let config = {
  oid: 1,
  host: process.env.NODE_ENV === 'production' ? `${protocol}//cdb.workair.vip/operate` : '',
  headerkey: "quanchong&tcp",
};

function $http(url, data = {}) {
 
  let loading = Loading.service({
    lock: true,
    text: "Loading",
    spinner: "el-icon-loading",
    background: "rgba(0,0,0,0.2)",
  });
  return new Promise((resolve, reject) => {
    axios({
        url: config.host + url,
        method: "post",
        headers: {
          token: store.state.token,
          ocode: store.state.ocode,
        },
        data,
      })
      .then((res) => {
        if (!res.data) {
          loading.close();
          Message.error("error");
        }
        let code = Number(res.data.code);
        switch (code) {
          case 0:
            loading.close();
            Message.error(res.data.msg);
            // reject(res.data)
            break;
          case 206:   
            loading.close();
            resolve(res.data);
            if (url != '/Auth/login') {
              router.replace({
                name: "payment",
                query: {
                  DD: JSON.stringify(res.data)
                }
              });
            }
            break;
          case 401:
            loading.close();
            Message.error(res.data.msg);
            logout();
            break;
          case 403:
            loading.close();
            Message.error(res.data.msg);
            logout();
            break;
          case 500:
            Message.error("error");
            break;
          default:
            loading.close();
            resolve(res.data);
            // if (url != '/Auth/login'){
            //   router.replace({
            //     name: "payment",
            //     query: {
            //       DD: JSON.stringify(res.data)
            //     }
            //   });
            // }

            break;
        }
      })
      .catch((error) => {
        loading.close();
        reject(error);
      });
  });
}

function $http2(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios({
        url: config.host + url,
        method: "post",
        headers: {
          token: store.state.token,
          ocode: store.state.ocode,
        },
        data,
      })
      .then((res) => {
        if (!res.data) {
          Message.error("error");
        }
        let code = Number(res.data.code);
        switch (code) {
          case 0:
            Message.error(res.data.msg);
            break;
          case 401:
            Message.error(res.data.msg);
            logout();
            break;
          case 403:
            Message.error(res.data.msg);
            logout();
            break;
          case 500:
            Message.error("error");
            break;
          default:
            resolve(res.data);
            break;
        }
      })
      .catch((error) => {
        reject(error);
      });
  });
}

function logout() {
  if (store.state.lang === "zh") {
    let arr = [{
      icon: "iconpingmu",
      hiddenIcon: false,
      title: "首页",
      index: "/index",
    }, ];
    store.commit("clearMenuArray", arr);
    store.commit("setActiveMenu", arr);
  } else if (store.state.lang === "en") {
    let arrE = [{
      icon: "iconpingmu",
      hiddenIcon: false,
      title: "Home",
      index: "/index",
    }, ];
    store.commit("clearMenuArray", arrE);
    store.commit("setActiveMenu", arrE);
  }
  store.commit("clearKeepAlive");
  localStorage.removeItem("zd_sky");
  router.replace({
    path: "/login"
  });
}

function formatTime(t) {
  let seconds = t % 60;
  let minutes = parseInt((t / 60) % 60);
  let hours = parseInt((t / 60 / 60) % 60);
  if (seconds < 10) {
    seconds = "0" + seconds;
  }
  if (minutes < 10) {
    minutes = "0" + minutes;
  }
  if (hours < 10) {
    hours = "0" + hours;
  }
  let res = hours + ":" + minutes + ":" + seconds;
  return res;
}

function getDate(type) {
  let oneDayLong = 24 * 60 * 60 * 1000;
  let now = new Date(); //当前日期
  let nowMonth = now.getMonth(); //当前月
  let nowYear = now.getFullYear(); //当前年
  let nowDay = now.getDay(); //当前一周中的某一天

  switch (type) {
    case "yesterday":
      let yesterdayTime = now.getTime() - oneDayLong;
      let yesterday = new Date(yesterdayTime);
      return [formatDate(yesterday), formatDate(yesterday)];
      break;
    case "today":
      return [formatDate(now), formatDate(now)];
      break;
    case "lastWeek":
      let lastMondayTime = now.getTime() - (nowDay + 7) * oneDayLong;
      let lastSundayTime = now.getTime() - (nowDay + 1) * oneDayLong;
      let lastMonday = new Date(lastMondayTime);
      let lastSunday = new Date(lastSundayTime);
      return [formatDate(lastMonday), formatDate(lastSunday)];
      break;
    case "week":
      let mondayTime = now.getTime() - nowDay * oneDayLong;
      let sundayTime = now.getTime() + (6 - nowDay) * oneDayLong;
      let monday = new Date(mondayTime);
      let sunday = new Date(sundayTime);
      return [formatDate(monday), formatDate(sunday)];
      break;
    case "lastMonth":
      if (nowMonth == 0) {
        nowYear = nowYear - 1;
        nowMonth = 12;
      }
      let lastMonthStart = new Date(nowYear, nowMonth - 1, 1);
      let lastMonthEnd = new Date(nowYear, nowMonth, 0);
      return [formatDate(lastMonthStart), formatDate(lastMonthEnd)];
      break;
    case "month":
      let monthStart = new Date(nowYear, nowMonth, 1);
      let monthEnd = new Date(nowYear, nowMonth + 1, 0);
      return [formatDate(monthStart), formatDate(monthEnd)];
      break;
    case "lastYear":
      let lastYearStart = new Date(nowYear - 1, 0, 1);
      let lastYearEnd = new Date(nowYear - 1, 12, 0);
      return [formatDate(lastYearStart), formatDate(lastYearEnd)];
      break;
    case "year":
      let yearStart = new Date(nowYear, 0, 1);
      let yearEnd = new Date(nowYear, 12, 0);
      return [formatDate(yearStart), formatDate(yearEnd)];
      break;
    default:
      break;
  }
}

function formatDate(date) {
  let myyear = date.getFullYear();
  let mymonth = date.getMonth() + 1;
  let myweekday = date.getDate();

  if (mymonth < 10) {
    mymonth = "0" + mymonth;
  }
  if (myweekday < 10) {
    myweekday = "0" + myweekday;
  }
  return myyear + "-" + mymonth + "-" + myweekday;
}

export default {
  config,
  $http,
  $http2,
  formatTime,
  getDate
};